//
//  RootViewController.m
//  sxwphone
//
//  Created by caijinglong on 2017/7/15.
//  Copyright © 2017年 Facebook. All rights reserved.
//

#import "RootViewController.h"
#import "RCTViewDelegate.h"
#import <UIKit/UIKit.h>
#import <React/RCTRootView.h>
#import <React/RCTBundleURLProvider.h>
#import <React/RCTBridge.h>
#import "RouteModule.h"

@interface RootViewController ()

@end

@implementation RootViewController

RCT_EXPORT_MODULE();

- (NSURL *)sourceURLForBridge:(RCTBridge *)bridge {
  NSURL *jsCodeLocation;
  
  jsCodeLocation = [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:self.jsName fallbackResource:nil];
  
  return jsCodeLocation;
}

- (instancetype)init {
  self = [super init];
  if (self) {
    NSLog(@"首页面创建");
  }
  return self;
}

- (void)viewDidLoad {
  [super viewDidLoad];
  // Do any additional setup after loading the view.
  NSLog(@"开启首页面");
  
}

-(void)viewWillAppear:(BOOL)animated{
  [super viewWillAppear:animated];
  
}

- (void)didReceiveMemoryWarning {
  [super didReceiveMemoryWarning];
  // Dispose of any resources that can be recreated.
}

RCT_EXPORT_METHOD(newPageWithJsName:
                  (NSString *) jsName
                  moduleName:
                  (NSString *) moduleName params:(NSDictionary *) params){
  NSLog(@"jsName = %@  moduleName = %@", jsName, moduleName);
  dispatch_async(dispatch_get_main_queue(), ^{
    RootViewController *ctl = [RootViewController newViewCtlWithJsName:jsName moduleName:moduleName params:params];
    
    [self presenterViewController:ctl];
  });
}

- (void)presenterViewController:(UIViewController *)ctl {
  
  [self presentViewController:ctl animated:YES completion:^{
    NSLog(@"开启新页面");
  }];
  //  });
}

+ (RootViewController *)newViewCtlWithJsName:(NSString *)jsName moduleName:(NSString *)moduleName params:(NSDictionary*) params{
  RootViewController *ctl = [RootViewController new];
  ctl.jsName = jsName;
  RCTBridge *bridge = [[RCTBridge alloc] initWithDelegate:ctl launchOptions:nil];
  RCTRootView *rootView = [[RCTRootView alloc] initWithBridge:bridge moduleName:moduleName initialProperties:params];
  ctl.view = rootView;
  ctl.delegate = [RCTViewDelegate instanceWithRctView:rootView rootViewController:ctl];
  return ctl;
}

+ (RootViewController *)presentFromViewController:(UIViewController*)viewCtl jsName:(NSString*)jsName moduleName:(NSString*) moduleName params:(NSDictionary*)params{
  RootViewController *ctl = [self newViewCtlWithJsName:jsName moduleName:moduleName params:params];
  [viewCtl presentViewController:ctl animated:YES completion:^{
  }];
  
  [RouteModule addViewDelegate:ctl.delegate];
  
  return ctl;
}

+ (void)dismissViewController:(RootViewController*)viewCtl {
  [viewCtl dismissViewControllerAnimated:YES completion:^{
    [RouteModule removeViewDelegate:viewCtl.delegate];
  }];
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
